package com.yaowk.service.mall.clothing.service.impl;

import com.yaowk.api.common.PageReq;
import com.yaowk.api.common.PageResp;
import com.yaowk.api.mall.clothing.model.Store;
import com.yaowk.api.mall.clothing.service.IStoreService;
import com.yaowk.api.mall.clothing.web.StoreAuditModel;
import com.yaowk.api.mall.clothing.web.StoreAuditQueryModel;
import com.yaowk.api.mall.clothing.web.StoreModel;
import com.yaowk.api.utils.DateUtils;
import com.yaowk.service.common.BaseRepository;
import com.yaowk.service.common.BaseServiceImpl;
import com.yaowk.service.file.entity.TSource;
import com.yaowk.service.mall.clothing.common.StatusDict;
import com.yaowk.service.mall.clothing.dao.OrderDao;
import com.yaowk.service.mall.clothing.dao.StoreDao;
import com.yaowk.service.mall.clothing.entity.CmStore;
import com.yaowk.service.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.persistence.Transient;
import java.util.*;

@Service
public class StoreServiceImpl extends BaseServiceImpl<Store, CmStore, Integer> implements IStoreService {

    @Autowired
    private StoreDao dao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private DateUtils dateUtils;
    @Autowired
    private PageUtils pageUtils;

    @Override
    protected BaseRepository getDao() {
        return dao;
    }

    @Override
    protected CmStore model2Entity(Store model) {
        CmStore cmStore = null;
        if (isNotEmpty(model)) {
            cmStore = new CmStore();
            cmStore.setAccount(model.getAccount());
            cmStore.setAddress(model.getAddress());
            cmStore.setCode(model.getCode());
            cmStore.setHeadUrl(model.getHeadUrl());
            cmStore.setLatitude(model.getLatitude());
            cmStore.setLongitude(model.getLongitude());
            cmStore.setName(model.getName());
            cmStore.setPhoneNumber(model.getPhoneNumber());
            cmStore.setRealName(model.getRealName());
            cmStore.setRemark(model.getRemark());
            cmStore.setCreateTime(model.getCreateTime());
            cmStore.setId(model.getId());
            cmStore.setPassword(model.getPassword());
            cmStore.setStatus(model.getStatus());
            cmStore.setUsername(model.getUsername());
        }
        return cmStore;
    }

    @Override
    protected StoreModel entity2model(CmStore entity) {
        StoreModel storeModel = null;
        if (isNotEmpty(entity)) {
            storeModel = new StoreModel();
            storeModel.setAccount(entity.getAccount());
            storeModel.setAddress(entity.getAddress());
            storeModel.setCode(entity.getCode());
            storeModel.setHeadUrl(entity.getHeadUrl());
            storeModel.setLatitude(entity.getLatitude());
            storeModel.setLongitude(entity.getLongitude());
            storeModel.setName(entity.getName());
            storeModel.setPhoneNumber(entity.getPhoneNumber());
            storeModel.setRealName(entity.getRealName());
            storeModel.setRemark(entity.getRemark());
            storeModel.setCreateTime(entity.getCreateTime());
            storeModel.setId(entity.getId());
            storeModel.setPassword(entity.getPassword());
            storeModel.setStatus(entity.getStatus());
            storeModel.setUsername(entity.getUsername());
        }
        return storeModel;
    }

    @Override
    public PageResp<StoreModel> findAllOfWeb(String keyword, PageReq pageReq) {

        // 查询列表
        PageRequest pageRequest = pageUtils.pageReq2PageRequest(pageReq);
        CmStore condition = new CmStore();
        if (isNotEmpty(keyword)) {
            keyword = "%" + keyword + "%";
            condition.setName(keyword);
        }
        Page<CmStore> page = dao.findAll(Example.of(condition), pageRequest);

        // 构造返回
        PageResp<StoreModel> pageResp = pageUtils.page2PageResp(page);

        // 计算时间区间
        Date now = new Date();
        Date todayBegin = dateUtils.getBeginTimeOfDay(now);
        Date todayEnd = dateUtils.getEndTimeOfDay(now);
        Date monthBegin = dateUtils.getBeginTimeOfMonth(now);
        Date monthEnd = dateUtils.getEndTimeOfMonth(now);

        // 查询订单量
        List<StoreModel> storeModels = pageResp.getList();
        for (CmStore cmStore : page.getContent()) {
            StoreModel storeModel = entity2model(cmStore);
            storeModel.setTodayOrderNum(orderDao.countByStoreIdAndCreateTimeBetween(cmStore.getId(), todayBegin, todayEnd));
            storeModel.setMonthOrderNum(orderDao.countByStoreIdAndCreateTimeBetween(cmStore.getId(), monthBegin, monthEnd));
            storeModels.add(storeModel);
        }

        return pageResp;
    }

    @Override
    public Store register(Store store, List<Long> sourceIds) {
        return null;
    }


    @Override
    public PageResp<StoreAuditModel> findAllOfAudit(StoreAuditQueryModel queryModel, PageReq pageReq) {
        // 查询列表
        PageRequest pageRequest = pageUtils.pageReq2PageRequest(pageReq);
        CmStore condition = new CmStore();
        if (isNotEmpty(queryModel.getKeyword())) {
            String keyword = "%" + queryModel.getKeyword() + "%";
            condition.setName(keyword);
        }
        if (isNotEmpty(queryModel.getStatus())) {
            condition.setStatus(queryModel.getStatus());
        }
        Page<CmStore> page = dao.findAll(Example.of(condition), pageRequest);

        // 构造返回
        PageResp<StoreAuditModel> pageResp = pageUtils.page2PageResp(page);

        List<StoreAuditModel> storeAuditModels = pageResp.getList();
        for (CmStore cmStore : page.getContent()) {
            StoreAuditModel storeAuditModel = new StoreAuditModel();
            storeAuditModels.add(storeAuditModel);

            storeAuditModel.setAccount(cmStore.getAccount());
            storeAuditModel.setStatus(cmStore.getStatus());
            storeAuditModel.setCode(cmStore.getCode());
            storeAuditModel.setName(cmStore.getName());
            storeAuditModel.setRealName(cmStore.getRealName());
            storeAuditModel.setPhoneNumber(cmStore.getPhoneNumber());
            storeAuditModel.setCreateTime(cmStore.getCreateTime());
            storeAuditModel.setAddress(cmStore.getAddress());

            // 封装证件图片路径
            List<String> certificates = new ArrayList<>();
            storeAuditModel.setCertificates(certificates);


        }
        return pageResp;
    }

    @Override
    @Transient
    public void auditSuccess(Integer id) {
        CmStore cmStore = dao.findOne(id);
        cmStore.setStatus(StatusDict.SUCCESS_AUDIT);
        dao.save(cmStore);
    }

    @Override
    @Transient
    public void auditFail(Integer id, String remark) {
        CmStore cmStore = dao.findOne(id);
        cmStore.setStatus(StatusDict.FAIL_AUDIT);
        cmStore.setRemark(remark);
        dao.save(cmStore);
    }
}
